package cn.iocoder.yudao.module.planet.service.impl;

import cn.iocoder.yudao.module.planet.controller.app.dto.PlanetDTO;
import cn.iocoder.yudao.module.planet.controller.app.dto.PlanetCreateReqDTO;
import cn.iocoder.yudao.module.planet.controller.app.dto.PlanetUpdateReqDTO;
import cn.iocoder.yudao.module.planet.dal.dataobject.PlanetDO;
import cn.iocoder.yudao.module.planet.dal.mapper.PlanetMapper;
import cn.iocoder.yudao.module.planet.service.PlanetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 星球服务实现类
 * 
 * @author 系统生成
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PlanetServiceImpl implements PlanetService {

    @Resource
    private PlanetMapper planetMapper;

    @Override
    public Long createPlanet(PlanetCreateReqDTO createReqDTO) {
        // 构建星球DO对象
        PlanetDO planetDO = new PlanetDO();
        planetDO.setTenantId(createReqDTO.getTenantId());
        planetDO.setName(createReqDTO.getName());
        planetDO.setDescription(createReqDTO.getDescription());
        planetDO.setCreatedAt(LocalDateTime.now());
        planetDO.setUpdatedAt(LocalDateTime.now());

        // 插入数据库
        planetMapper.insert(planetDO);

        return planetDO.getId();
    }

    @Override
    public void updatePlanet(PlanetUpdateReqDTO updateReqDTO) {
        // 查询星球信息
        PlanetDO planetDO = planetMapper.selectById(updateReqDTO.getId());
        if (planetDO == null) {
            throw new RuntimeException("星球不存在: " + updateReqDTO.getId());
        }

        // 更新星球信息
        planetDO.setName(updateReqDTO.getName());
        planetDO.setDescription(updateReqDTO.getDescription());
        planetDO.setUpdatedAt(LocalDateTime.now());

        // 保存到数据库
        planetMapper.updateById(planetDO);
    }

    @Override
    public void deletePlanet(Long id) {
        // 删除星球
        planetMapper.deleteById(id);
    }

    @Override
    public PlanetDTO getPlanet(Long id) {
        // 查询星球信息
        PlanetDO planetDO = planetMapper.selectById(id);
        if (planetDO == null) {
            return null;
        }

        // 转换为DTO对象
        return convertToDTO(planetDO);
    }

    @Override
    public List<PlanetDTO> getPlanetListByTenantId(Long tenantId) {
        // 查询星球列表
        List<PlanetDO> planetDOList = planetMapper.selectListByTenantId(tenantId);

        // 转换为DTO列表
        return planetDOList.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 将PlanetDO转换为PlanetDTO
     */
    private PlanetDTO convertToDTO(PlanetDO planetDO) {
        PlanetDTO planetDTO = new PlanetDTO();
        planetDTO.setId(planetDO.getId());
        planetDTO.setTenantId(planetDO.getTenantId());
        planetDTO.setName(planetDO.getName());
        planetDTO.setDescription(planetDO.getDescription());
        planetDTO.setCreatedAt(planetDO.getCreatedAt());
        planetDTO.setUpdatedAt(planetDO.getUpdatedAt());
        return planetDTO;
    }

}